{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'pycountry'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-ad75fd07f741>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mlocale\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[1;31m#国家的编码、名字、语言、货币等信息\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpycountry\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pycountry'"
     ]
    }
   ],
   "source": [
    "# 特征编码工具\n",
    "#该事件涉及国家、城市、时间等信息的处理\n",
    "\n",
    "#保存数据\n",
    "import pickle\n",
    "\n",
    "#特征编码\n",
    "import datetime\n",
    "import hashlib\n",
    "# POSIX locale database and functionality\n",
    "import locale\n",
    "#国家的编码、名字、语言、货币等信息\n",
    "import pycountry\n",
    "\n",
    "import numpy as np\n",
    "import scipy.io as sio\n",
    "import scipy.sparse as ss\n",
    "\n",
    "from collections import defaultdict\n",
    "from sklearn.preprocessing import normalize\n",
    "\n",
    "#类别型特征编码\n",
    "#这里写成类的形式，因为编码字典要在不同的文件中使用\n",
    "class FeatureEng:\n",
    "  def __init__(self):\n",
    "    \n",
    "    # 载入 locales\n",
    "    self.localeIdMap = defaultdict(int)\n",
    "    for i, l in enumerate(locale.locale_alias.keys()):\n",
    "      self.localeIdMap[l] = i + 1\n",
    "    #print locale.locale_alias.keys()\n",
    "\n",
    "    # 载入 countries\n",
    "    self.countryIdMap = defaultdict(int)\n",
    "    ctryIdx = defaultdict(int)\n",
    "    for i, c in enumerate(pycountry.countries):\n",
    "      self.countryIdMap[c.name.lower()] = i + 1\n",
    "      if c.name.lower() == \"usa\":\n",
    "        ctryIdx[\"US\"] = i\n",
    "      if c.name.lower() == \"canada\":\n",
    "        ctryIdx[\"CA\"] = i\n",
    "    for cc in ctryIdx.keys():\n",
    "      for s in pycountry.subdivisions.get(country_code=cc):\n",
    "        self.countryIdMap[s.name.lower()] = ctryIdx[cc] + 1\n",
    "        \n",
    "    # 载入 gender id 字典\n",
    "    ##缺失补0，性别未知\n",
    "    self.genderIdMap = defaultdict(int, {'NaN': 0, \"male\":1, \"female\":2})\n",
    "\n",
    "  def getLocaleId(self, locstr):\n",
    "    return self.localeIdMap[locstr.lower()]\n",
    "\n",
    "  def getGenderId(self, genderStr):\n",
    "    return self.genderIdMap[genderStr]\n",
    "\n",
    "  def getJoinedYearMonth(self, dateString):\n",
    "    try:\n",
    "        dttm = datetime.datetime.strptime(dateString, \"%Y-%m-%dT%H:%M:%S.%fZ\")\n",
    "        return (dttm.year-2010)*12 + dttm.month\n",
    "    except:  #缺失补0\n",
    "        return 0\n",
    "\n",
    "  def getCountryId(self, location):\n",
    "    if (isinstance(location, str)\n",
    "        and len(location.strip()) > 0\n",
    "        and location.rfind(\"  \") > -1):\n",
    "        return self.countryIdMap[location[location.rindex(\"  \") + 2:].lower()]\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "  def getBirthYearInt(self, birthYear):\n",
    "    try:\n",
    "      return 0 if birthYear == \"None\" else int(birthYear)\n",
    "    except:\n",
    "      return 0\n",
    "\n",
    "  def getTimezoneInt(self, timezone):\n",
    "    try:\n",
    "      return int(timezone)\n",
    "    except:\n",
    "      return 0\n",
    "\n",
    "  def getFeatureHash(self, value):\n",
    "    if len(value.strip()) == 0:\n",
    "      return -1\n",
    "    else:\n",
    "      return int(hashlib.sha224(value).hexdigest()[0:4], 16)\n",
    "\n",
    "  def getFloatValue(self, value):\n",
    "    if len(value.strip()) == 0:\n",
    "      return 0.0\n",
    "    else:\n",
    "      return float(value)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
